﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class ClassSpecificationDAL : IClassSpecificationDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public ClassSpecificationDAL()
        {

        }
        public DataSet LoadClassSpecification(Nullable<int> Sequence, Nullable<int> PositionTypes)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@PositionTypes", PositionTypes);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT s.*"
                    + ", pt.POS_TYPE_NAME"
                    + " FROM HRM_MP_CLASS_SPECIFICATIONS s"
                    + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON s.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                    + " WHERE s.RECORD_STATUS not like @DeleteRecord"
                    + (Sequence.HasValue ? " AND s.CLASS_SPEC_SEQ = @Sequence" : "")
                    + (PositionTypes.HasValue ? " AND s.POS_TYPE_SEQ = @PositionTypes" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        
        public IList<ClassSpecificationInfo> ListClassSpecification(Nullable<int> Sequence, Nullable<int> PositionTypes)
        {
            string strSQL;
     
            List<ClassSpecificationInfo> infoList = new List<ClassSpecificationInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@PositionTypes", PositionTypes);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT CLASS_SPEC_SEQ, CLASS_SPEC_NAME, CLASS_SPEC_ENG"
                        + ", POS_TYPE_SEQ, RECORD_STATUS, CREATED_BY, CREATED_WHEN"
                    + " FROM HRM_MP_CLASS_SPECIFICATIONS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (Sequence.HasValue ? " AND CLASS_SPEC_SEQ = @Sequence" : "")
                    + (PositionTypes.HasValue ? " AND POS_TYPE_SEQ = @PositionTypes" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    PositionTypeDAL posDAL = new PositionTypeDAL();
                    PositionTypeInfo posInfo = new PositionTypeInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<PositionTypeInfo> posList = posDAL.ListPositionTypes(rdr.IsDBNull(3) ? (int?)null : rdr.GetInt32(3), null);
                        posInfo = posList.Count > 0 ? posList[0] : null;
                    }

                    ClassSpecificationInfo Info = new ClassSpecificationInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , posInfo
                            , ""
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        
        public void Insert(ClassSpecificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@ClassSpecName", Info.Name);
                dbManager.AddParameters(1, "@ClassSpecEng", Info.NameEng);
                dbManager.AddParameters(2, "@PosTypeSeq", Info.PositionTypes.Sequence);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_CLASS_SPECIFICATIONS(CLASS_SPEC_NAME, CLASS_SPEC_ENG"
                        + ", POS_TYPE_SEQ, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@ClassSpecName, @ClassSpecEng, @PosTypeSeq, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Update(ClassSpecificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@ClassSpecName", Info.Name);
                dbManager.AddParameters(2, "@ClassSpecEng", Info.NameEng);
                dbManager.AddParameters(3, "@PosTypeSeq", Info.PositionTypes.Sequence);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_CLASS_SPECIFICATIONS"
                           + " SET CLASS_SPEC_NAME = @ClassSpecName"
                                + ", CLASS_SPEC_ENG = @ClassSpecEng"
                                + ", POS_TYPE_SEQ = @PosTypeSeq"
                                + ", RECORD_STATUS = @RecordStatus"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE CLASS_SPEC_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_MP_CLASS_SPECIFICATIONS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE CLASS_SPEC_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
